// ToastManager.ts
import { _decorator, instantiate, Component, Prefab, resources, UITransform, Vec3 } from 'cc';
import { Toast } from './Toast';
const { ccclass, property } = _decorator;

@ccclass('ToastManager')
export class ToastManager extends Component {
    private static _instance: ToastManager = null;

    static show(message: string, duration: number = 2) {
        if (!this._instance) {
            console.warn('ToastManager not initialized.');
            return;
        }
        this._instance._showToast(message, duration);
    }

    @property(Prefab)
    toastPrefab: Prefab = null;

    protected onLoad(): void {
        ToastManager._instance = this;
    }

    private _showToast(message: string, duration: number) {
        const toastNode = instantiate(this.toastPrefab);
        toastNode.setParent(this.node);

        // 居中显示，可自定义位置
        toastNode.setPosition(new Vec3(0, -200, 0)); // 例如底部提示
        toastNode.getComponent(Toast).show(message, duration);
    }
}